package com.lsht.system.service.impl;

import com.lsht.system.domain.siwei.SiweiJwd;
import com.lsht.system.mapper.siwei.SiweiJwdMapper;
import com.lsht.system.service.siwei.SiweiJwdService;
import com.lsht.system.util.NullUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zyc
 * @date 2024/11/17 14:15
 */
@Service
public class SiweiJwdServiceImpl implements SiweiJwdService {
    @Autowired
    private SiweiJwdMapper siweiJwdMapper;

    /**
     * 新增区域自动回显
     *
     * @param siweiJwd 区域自动回显
     * @return 结果
     */
    @Override
    public int genAdd(SiweiJwd siweiJwd) {
        return siweiJwdMapper.genAdd(siweiJwd);
    }

    /**
     * 删除区域自动回显信息
     *
     * @param id 区域自动回显主键
     * @return 结果
     */
    @Override
    public int genDeleteById(List<String> idList) {
        return siweiJwdMapper.genDeleteById(idList);
    }

    /**
     * 修改区域自动回显
     *
     * @param siweiJwd 区域自动回显
     * @return 结果
     */
    @Override
    public int genUpdateById(SiweiJwd siweiJwd) {
        return siweiJwdMapper.genUpdateById(siweiJwd);
    }

    /**
     * 查询区域自动回显列表
     *
     * @param siweiJwd 区域自动回显
     * @return 区域自动回显
     */
    @Override
    public List<SiweiJwd> genQueryList(SiweiJwd siweiJwd) {
        return siweiJwdMapper.genQueryList(siweiJwd);
    }

    @Override
    public String getAreaNameByPosition(SiweiJwd siweiJwd) {
        // 假设SiweiJwd类有areaName属性
        List<SiweiJwd> list = siweiJwdMapper.getAllData();
        Double lan = siweiJwd.getLan();
        Double lat = siweiJwd.getLat();
// 地球半径
        double R = 6371;
// 用来存储每个位置的距离和对应的 areaName
        List<Double> distances = new ArrayList<>();
        List<String> areaNames = new ArrayList<>();
        if (NullUtil.isNotEmpty(list) && list.size() > 0) {
            for (SiweiJwd jwd : list) {
                Double lan1 = jwd.getLan();
                Double lat1 = jwd.getLat();
                if (NullUtil.isNotNull(lan1) && NullUtil.isNotNull(lat1)) {
                    // 计算两点之间的距离
                    double value = Math.acos(Math.sin(lat) * Math.sin(lat1) + Math.cos(lat) * Math.cos(lat1) * Math.cos(lan1 - lan)) * R;
                    double roundedValue = round(value * 1000, 2); // 转换为米并保留两位小数
                    // 存储距离和对应的areaName
                    distances.add(roundedValue);
                    areaNames.add(jwd.getAreaName());  // 假设getAreaName()可以获取区域名
                }
            }
            // 如果列表不为空，找到最小的距离和对应的areaName
            if (!distances.isEmpty()) {
                double minDistance = distances.stream().min(Double::compareTo).get();  // 找到最小的距离
                int minIndex = distances.indexOf(minDistance);  // 获取最小距离的索引
                String minAreaName = areaNames.get(minIndex);  // 获取最小距离对应的区域名
                // 打印最小距离和对应的区域名
                System.out.println("最小距离: " + minDistance + " 米, 对应区域: " + minAreaName);
                return minAreaName;  // 返回最小距离对应的areaName
            }
        }

        return "";  // 如果没有找到有效的数据，返回空字符串
    }

    public static double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();
        BigDecimal bd = BigDecimal.valueOf(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }
}
